<?php
include "TitleInsuranceCalculator.php";
include "ReissueTitleInsuranceCost.php";
include "PurchaseTitleInsuranceCost.php";
include "RefinanceTitleInsuranceCost.php";
include "Estate.php";
include "MDRecordationAndTransferTaxCalculator.php";
include "VARecordationAndTransferTaxCalculator.php";
include "DCRecordationAndTransferTaxCalculator.php";
include "DERecordationAndTransferTaxCalculator.php";
include "FLRecordationAndTransferTaxCalculator.php";
include "RateInfoProperties.php";
include "CountyLocale.php";
include "TownLocale.php";
include "FirstTimeHomeBuyer.php";
include "PrimaryResidence.php";
include "ERequestedPayer.php";
include "StatePropertyTransferTax.php";
include "FeeCalculator.php";
include "Fee.php";
include "TierCost.php";
include "RecordationAndTransferTaxCreator.php";
include "BuyerRecordationAndTransferTaxFactory.php";
include "SellerRecordationAndTransferTaxFactory.php";
include "Split50RecordationAndTransferTaxFactory.php";
include "VADefaultRecordationAndTransferTaxFactory.php";
include "FLDefaultRecordationAndTransferTaxFactory.php";
include "FeeHelper.php";
include "RecordationTax.php";
include "CountyPropertyTransferTax.php";
include "RecordationAndTransferTaxes.php";
include "EPayer.php";
include "PurchaseGFE.php";
include "GFEReportCreater.php";
include "EPayerSpecialCase.php";
include "Payoff.php";
include "AbstractRefinanceGFE.php";
include "RefinanceGFE.php";
include "RefinanceReissueGFE.php";
include "ExistingOwnersPolicy.php";
include "GenericBoolean.php";


class TaxCaculate
{
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    public static function calculatePurchaseGFE($state, $countyLocale, $townLocale, $consideration, $loanAmounts, $enhancedCoverage, $qualifiedFirstTimeBuyer, $ownerOccupiedResidence, $requestedPayer) 
    {
        $rateInfoProperties = new RateInfoProperties();
        $rateInfoProperties->loadDatabase();

        $testTitleInsuranceCalculator = new TitleInsuranceCalculator($state, $rateInfoProperties);
        //$result = new PurchaseTitleInsuranceCost();

        $purchaseTitleInsuranceCost = $testTitleInsuranceCalculator->purchase($consideration, $loanAmounts, $enhancedCoverage);
        //echo "Owner cost: ".$purchaseTitleInsuranceCost->getOwnersCost()." Lender cost: ".$purchaseTitleInsuranceCost->getLendersCost()."<br/>";

        //$result = $testTitleInsuranceCalculator->refinance(array(500000));
        //echo "Refinance cost: ".$result->getRefinanceCost()."<br/>";
        
        switch ($state)
        {
            case EState::MD: 
                $testRecordationCalculator = new MDRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::VA: 
                $testRecordationCalculator = new VARecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::DC: 
                $testRecordationCalculator = new DCRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::DE: 
                $testRecordationCalculator = new DERecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::FL: 
                $testRecordationCalculator = new FLRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            default:
                $testRecordationCalculator = new MDRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
        }        

        $transferTaxeses = $testRecordationCalculator->calculatePurchaseRecordationAndTransferTax($countyLocale, $townLocale, $consideration, $loanAmounts, $qualifiedFirstTimeBuyer, $ownerOccupiedResidence, $requestedPayer);
        //if ($transferTaxeses[0]->getRecordationTax() != null)
            //echo "Recordation: ".$transferTaxeses[0]->getRecordationTax()->getAmount()."<br/>";

        return new PurchaseGFE($state, $countyLocale, $townLocale, $consideration, $loanAmounts, $ownerOccupiedResidence,
                        $enhancedCoverage, $qualifiedFirstTimeBuyer, $requestedPayer, $purchaseTitleInsuranceCost, $transferTaxeses);

    }
    
    public static function calculateRefinanceGFE($state, $countyLocale, $newLoanAmounts, $ownerOccupiedResidence, $payoffAmounts, $ownersPolicyAmount, $sameLender, $mobileClosing, $genericBooleans) {
        $rateInfoProperties = new RateInfoProperties();
        $rateInfoProperties->loadDatabase();

        $testTitleInsuranceCalculator = new TitleInsuranceCalculator($state, $rateInfoProperties);
        
        $refinanceTitleInsuranceCost = $testTitleInsuranceCalculator->refinance($newLoanAmounts);

        switch ($state)
        {
            case EState::MD: 
                $testRecordationCalculator = new MDRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::VA: 
                $testRecordationCalculator = new VARecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::DC: 
                $testRecordationCalculator = new DCRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::DE: 
                $testRecordationCalculator = new DERecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::FL: 
                $testRecordationCalculator = new FLRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            default:
                $testRecordationCalculator = new MDRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
        }
        $transferTaxes = $testRecordationCalculator->calculateRefinanceRecordationAndTransferTax($countyLocale, $newLoanAmounts, $ownerOccupiedResidence, $payoffAmounts, $sameLender);
        //print_r($transferTaxes->getFees()); exit();

        $mobileClosingCosts = TaxCaculate::calculateMobileClosingCosts($mobileClosing);

        return new RefinanceGFE($state, $countyLocale, null, $ownerOccupiedResidence, $newLoanAmounts, $payoffAmounts, $ownersPolicyAmount, $refinanceTitleInsuranceCost, $transferTaxes, $mobileClosingCosts, $genericBooleans);
    }
    private static function calculateMobileClosingCosts($mobileClosing) {
        $mobileClosingCosts = 0.0;
        if ($mobileClosing) {
            $mobileClosingCosts = 125.0;
        }
        return $mobileClosingCosts;
    }
    
    public static function calculateRefinanceReissueGFE($state, $countyLocale, $loanAmounts, $ownerOccupiedResidence, $payoffAmounts, $sameLender, $existingOwnersPolicy, $ownersPolicyAmount, $mobileClosing, $genericBooleans) {
        $rateInfoProperties = new RateInfoProperties();
        $rateInfoProperties->loadDatabase();

        $testTitleInsuranceCalculator = new TitleInsuranceCalculator($state, $rateInfoProperties);        
        
        $refinanceTitleInsuranceCost = $testTitleInsuranceCalculator->refinanceReissue($loanAmounts, $existingOwnersPolicy, $ownersPolicyAmount->getValue());
                
        switch ($state)
        {
            case EState::MD: 
                $testRecordationCalculator = new MDRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::VA: 
                $testRecordationCalculator = new VARecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::DC: 
                $testRecordationCalculator = new DCRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::DE: 
                $testRecordationCalculator = new DERecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            case EState::FL: 
                $testRecordationCalculator = new FLRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
                break;
            default:
                $testRecordationCalculator = new MDRecordationAndTransferTaxCalculator($state, $rateInfoProperties);
        }        
        $transferTaxes = $testRecordationCalculator->calculateRefinanceRecordationAndTransferTax($countyLocale, $loanAmounts, $ownerOccupiedResidence, $payoffAmounts, $sameLender);

        $mobileClosingCosts = TaxCaculate::calculateMobileClosingCosts($mobileClosing);

        return new RefinanceReissueGFE(
                $state,
                $countyLocale,
                null,
                $ownerOccupiedResidence,
                $loanAmounts,
                $payoffAmounts,
                $ownersPolicyAmount,
                $refinanceTitleInsuranceCost,
                $transferTaxes,
                $mobileClosingCosts,
                $genericBooleans);
    }
}

//$state = EState::MD;
//$countyLocale = new CountyLocale(CountyLocale::Montgomery);
//$townLocale = null;
//$consideration = 200000;
//$loanAmounts = array(100000);
//$enhancedCoverage = false;
//$qualifiedFirstTimeBuyer = new FirstTimeHomeBuyer(null, false);
//$ownerOccupiedResidence = new PrimaryResidence(null, true);
//$requestedPayer = ERequestedPayer::Buyer;

$transactionType = $_POST['transaction_type'];
$state = $_POST['state'];
$countyLocale = new CountyLocale($_POST['county_locale']);
$townLocale = null;
if (isset($_POST['town_locale']))
    $townLocale = new TownLocale ($_POST['town_locale']);
    
if (isset($_POST['purchase_price']) && floatval($_POST['purchase_price']) > 0)
    $consideration = floatval($_POST['purchase_price']);

$loanAmounts = array();
if ($transactionType == 'Purchase')
{
    if (isset($_POST['loan_amount1']) && floatval($_POST['loan_amount1']) > 0)
        $loanAmounts[] = floatval($_POST['loan_amount1']);

    if (isset($_POST['loan_amount2']) && floatval($_POST['loan_amount2']) > 0)
        $loanAmounts[] = floatval($_POST['loan_amount2']);
}
else
{
    if (isset($_POST['loan_amount3']) && floatval($_POST['loan_amount3']) > 0)
        $loanAmounts[] = floatval($_POST['loan_amount3']);

    if (isset($_POST['loan_amount4']) && floatval($_POST['loan_amount4']) > 0)
        $loanAmounts[] = floatval($_POST['loan_amount4']);
}

$payoffAmounts = array();
if (isset($_POST['paid_off1'])  && floatval($_POST['paid_off1']) > 0)
    $payoffAmounts[] = new Payoff (null, floatval($_POST['paid_off1']));

if (isset($_POST['paid_off2']) && floatval($_POST['paid_off2']) > 0)
    $payoffAmounts[] = new Payoff (null, floatval($_POST['paid_off2']));

$sameLender = null;
if (isset($_POST['same_lender']))
    $sameLender = $_POST['same_lender'];

$mobileClosing = null;
if (isset($_POST['mobile_closing']))
    $mobileClosing = $_POST['mobile_closing'];

$enhancedCoverage = $_POST['enhanced_coverage'];

$qualifiedFirstTimeBuyer = null;
if (isset($_POST['home_buyer']))
    if ($_POST['home_buyer'])
        $qualifiedFirstTimeBuyer = new FirstTimeHomeBuyer(null, true);
    else
        $qualifiedFirstTimeBuyer = new FirstTimeHomeBuyer(null, false);

$ownerOccupiedResidence  = null;
if (isset($_POST['primary_residence']))
    if ($_POST['primary_residence'])
        $ownerOccupiedResidence = new PrimaryResidence(null, true);
    else
        $ownerOccupiedResidence = new PrimaryResidence(null, false);

$existingOwnersField = false;
if (isset($_POST['chkowner']))
    if ($_POST['chkowner'])
        $existingOwnersField = true;

if (isset($_POST['pass_policy']))
    if ($_POST['pass_policy'])
        $existingOwnersField = true;    
    
$ownersPolicyAmount = null;
if (isset($_POST['amount_policy']) && floatval($_POST['amount_policy']) > 0)
    $ownersPolicyAmount = new ExistingOwnersPolicy (null, floatval($_POST['amount_policy']));

$genericBooleans = array();
if (isset($_POST['same_lender']))
    $genericBooleans[] = new GenericBoolean ("Same lender", $_POST['same_lender']);

if (isset($_POST['own_policy']))
    $genericBooleans[] = new GenericBoolean ("Borrower will provide evidence of owner's policy", $_POST['own_policy']);

if (isset($_POST['pass_policy']))
    $genericBooleans[] = new GenericBoolean ("The policy was issued within past 10 years", $_POST['pass_policy']);

$requestedPayer = $_POST['payer'];

if ($transactionType == 'Purchase')
{
    $purchaseGFE = TaxCaculate::calculatePurchaseGFE($state, $countyLocale, $townLocale, $consideration, $loanAmounts, $enhancedCoverage, $qualifiedFirstTimeBuyer, $ownerOccupiedResidence, $requestedPayer);

    $creator = new GFEReportCreater($transactionType);
    $creator->GeneratePurchasePDF($purchaseGFE);
}
else
{
    //reissue
    if ($existingOwnersField && $ownersPolicyAmount && $ownersPolicyAmount->getValue() > 0) 
    {
        if ($state == 'de')
        {            
            $refinanceReissueGFE = TaxCaculate::calculateRefinanceReissueGFE($state, $countyLocale, $loanAmounts, $ownerOccupiedResidence, $payoffAmounts, false, true, $ownersPolicyAmount, $mobileClosing, $genericBooleans);   
            $creator = new GFEReportCreater($transactionType);
            $reissue = true;
            $titleInsuranceCost = $refinanceReissueGFE->getReissueTitleInsuranceCost()->getNetPolicyCost();
            $creator->GenerateRefinancePDF($refinanceReissueGFE, $titleInsuranceCost, $reissue);
        }
        else if ($state == 'va')
        {
            $refinanceReissueGFE = TaxCaculate::calculateRefinanceReissueGFE($state, $countyLocale, $loanAmounts, null, $payoffAmounts, $sameLender, $existingOwnersPolicy, $ownersPolicyAmount, $mobileClosing, $genericBooleans);
            $creator = new GFEReportCreater($transactionType);
            $reissue = true;
            $titleInsuranceCost = $refinanceReissueGFE->getReissueTitleInsuranceCost()->getNetPolicyCost();
            $creator->GenerateRefinancePDF($refinanceReissueGFE, $titleInsuranceCost, $reissue);
        }
        // voi thong thuong MD, WSDC, FL
        else
        {            
            $refinanceReissueGFE = TaxCaculate::calculateRefinanceReissueGFE($state, $countyLocale, $loanAmounts, $ownerOccupiedResidence, $payoffAmounts, $sameLender, $existingOwnersField, $ownersPolicyAmount, $mobileClosing, null);   
            $creator = new GFEReportCreater($transactionType);
            $reissue = true;
            $titleInsuranceCost = $refinanceReissueGFE->getReissueTitleInsuranceCost()->getNetPolicyCost();
            $creator->GenerateRefinancePDF($refinanceReissueGFE, $titleInsuranceCost, $reissue);
        }
    }
    else
    {        
        //de
        if ($state == 'de')
            $refinanceGFE = TaxCaculate::calculateRefinanceGFE ($state, $countyLocale, $loanAmounts, $ownerOccupiedResidence, $payoffAmounts, null, false, $mobileClosing, null);
        //va
        else if ($state == 'va')
            $refinanceGFE = TaxCaculate::calculateRefinanceGFE ($state, $countyLocale, $loanAmounts, null, $payoffAmounts, null, $sameLender, $mobileClosing, null);
        else 
            $refinanceGFE = TaxCaculate::calculateRefinanceGFE ($state, $countyLocale, $loanAmounts, $ownerOccupiedResidence, $payoffAmounts, null, $sameLender, $mobileClosing, null);

        $creator = new GFEReportCreater($transactionType);
        $reissue = false;    
        $titleInsuranceCost = $refinanceGFE->getRefinanceTitleInsuranceCost()->getRefinanceCost();    
        $creator->GenerateRefinancePDF($refinanceGFE, $titleInsuranceCost, $reissue);
    }
}

?>
